<?php
include 'inc_common.php';
if($_POST){
    $data=$_POST;
}else{
    if($_GET){
        $data=$_GET;
    }
}
if (isset($data)) {
    open_conn();
    $result = mysql_query("select username,max_trunk as trunk,max_agent as agent,ip_address as ip,status from cetdb.cet where ip_address='" . $data['ip'] . "' limit 1") or die(mysql_error());
    $row = $result === false ? false : mysql_fetch_array($result, MYSQL_ASSOC);
    if (!$row) {
        $result = mysql_query("insert into cet (username, frist_runtime, ip_address, max_trunk, max_agent, status, last_runtime) values ('" . $data['username'] . "', now(), '" . $data['ip'] . "', " . $data['trunk'] . ", " . $data['agent'] . ", " . $data['status'] . ", now());");
    } else {
        if ($row['username'] <> $data['username'] || $row['trunk'] <> $data['trunk'] || $row['agent'] <> $data['agent']) {
            $result = mysql_query("update cet set username='" . $data['username'] . "', frist_runtime=now(), max_trunk=" . $data['trunk'] . ", max_agent=" . $data['agent'] . ", status=" . $data['status'] . ", last_runtime=now() where ip_address='" . $data['ip'] . "'");
        }
    }
    mysql_free_result($result);
    mysql_close($conn);
}
echo 'success';